package com.shanghaionstar.smart;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;
import com.shanghaionstar.user.adapter.OnstarFlowCommonAdapter;
import com.shanghaionstar.user.adapter.ViewHolder;

import java.util.Arrays;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartSelDistanceFrg extends BaseFragment implements View.OnClickListener {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.dis_lv)
    ListView mDisLv;

    private BaseActivity mActivity;
    String[] mDistance;

    private OnstarFlowCommonAdapter mAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        mTitleTv.setText(R.string.smart_trigger_distance);
        mDistance = getResources().getStringArray(R.array.smart_sel_dis);

        mAdapter = new OnstarFlowCommonAdapter<String>(mActivity, Arrays.asList(mDistance), R.layout.fragment_slk_select_dis_item) {

            @Override
            public void convert(ViewHolder holder, String s, int pos) {
                holder.setText(R.id.dis_tv, s);
                if (getClickPosition() == pos) {
                    holder.setVisible(R.id.sel_iv, true);
                } else {
                    holder.setVisible(R.id.sel_iv, false);
                }
            }
        };
        mDisLv.setAdapter(mAdapter);
        mDisLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mAdapter.setClickPosition(i);
                mAdapter.notifyDataSetChanged();
                if(mActivity instanceof  SmartHomeActivity){
                    ((SmartHomeActivity) mActivity).distance = mDistance[i];
                }
            }
        });


    }

    @OnClick({R.id.back_ib})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ib:
                back();
                break;
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_select_dis;
    }
}
